package com.crm.controller;

import com.crm.controller.util.R;
import com.crm.pojo.User;
import com.crm.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    private userService service;
    @GetMapping("/{tel}/{pwd}")
    public R login(@PathVariable String tel,@PathVariable String pwd){
        User user = new User();
        user.setTel(tel);
        user.setPassword(pwd);
        boolean flag = service.login(user);
        if(flag){
            User u = service.selectByTel(user);
            return new R(true,u,"登陆成功");
        }
        return new R(false,null,"登陆失败");
    }
    @PostMapping
    public R register(@RequestBody User user){
        // 判断该用户是否已存在
        User u = service.selectByTel(user);
        if(u==null){
            boolean flag = service.register(user);
            return new R(flag,null,flag? "注册成功":"注册失败");
        }
        return new R(false,null,"该用户已经存在");
    }
    @PutMapping
    public R updateUser(@RequestBody User user){
        boolean flag = service.updateUser(user);
        return new R(flag,null,flag? "更新成功":"更新失败");
    }
}
